package name.matthewgreet.example3.common; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class ListCache<K extends Serializable,T extends Serializable> implements Serializable { private static final long serialVersionUID = -8481658486616110929L; private static class ItemData<K,T> implements Serializable { private static final long serialVersionUID = 2092750648185163932L; private K id; // Placeholder id or primary of item private T item; // Record, which is null if not yet loaded private boolean pageExtensionReload; // Whether further data must be added to base record to display in a page private boolean reload; // Whether item must be loaded or reloaded. public ItemData() { id = null; item = null; pageExtensionReload = false; reload = false; } /** * Returns id or primary key of list item. */ public K getId() { return id; } public void setId(K value) { id = value; } public T getItem() { return item; } public void setItem(T value) { item = value; } /** * Returns whether additional record data not loaded by base record list finder need to be loaded to display a * page. This only applies in base record list pagination mode. */ public boolean getPageExtensionReload() { return pageExtensionReload; } public void setPageExtensionReload(boolean pageExtensionReload) { this.pageExtensionReload = pageExtensionReload; } /** * Whether item must be reloaded. */ public boolean getReload() { return reload; } public void setReload(boolean value) { reload = value; } } /** * Configuration set by concrete class to control template behaviour for lists. */ public static class ListCacheConfig<K extends Serializable,T extends Serializable> { private IdListFinder<K> idListFinder = null; private KeyExtractor<K,T> keyExtractor = null; private ListFinder<T> listFinder = null; private ListSizeFinder listSizeFinder = null; private String name; private PageByIdsFinder<K,T> pageByIdsFinder = null; private PageByIndexRangeFinder<T> pageByIndexRangeFinder = null; private PageExtensionAssembler<T> pageExtensionAssembler = null; private int pageSize = 10; private PaginationMode paginationMode = PaginationMode.FULL_LIST; public ListCacheConfig() { // Empty } /** * Returns finder Strategy for loading id list, if in page-by-id mode. */ public IdListFinder<K> getIdListFinder() { return idListFinder; } public void setIdListFinder(IdListFinder<K> value) { idListFinder = value; } /** * Returns adapter for obtaining unique key, usually the primary key, from records. */ public KeyExtractor<K, T> getKeyExtractor() { return keyExtractor; } public void setKeyExtractor(KeyExtractor<K, T> keyExtractor) { this.keyExtractor = keyExtractor; } /** * Returns list finder Strategy for loading full list, if in full list mode. */ public ListFinder<T> getListFinder() { return listFinder; } public void setListFinder(ListFinder<T> value) { listFinder = value; } /** * Returns finder Command for getting list size, if in page-by-index-range mode. */ public ListSizeFinder getListSizeFinder() { return listSizeFinder; } public void setListSizeFinder(ListSizeFinder listSizeFinder) { this.listSizeFinder = listSizeFinder; } /** * Returns name of this cache, used in logs. */ public String getName() { return name; } public void setName(String value) { name = value; } /** * Returns finder Command for loading multiple items by id, if in page-by-id mode. */ public PageByIdsFinder<K,T> getPageByIdsFinder() { return pageByIdsFinder; } public void setPageByIdsFinder(PageByIdsFinder<K,T> value) { pageByIdsFinder = value; } /** * Returns finder Command for loading multiple items by index range, if in page-by-index-range mode. */ public PageByIndexRangeFinder<T> getPageByIndexRangeFinder() { return pageByIndexRangeFinder; } public void setPageByIndexRangeFinder(PageByIndexRangeFinder<T> pageByIndexRangeFinder) { this.pageByIndexRangeFinder = pageByIndexRangeFinder; } /** * Returns Transfer Object Assembler for adding record extensions needed for requested page that is not loaded * by a base record list finder. Only applies for base record list pagination mode. */ public PageExtensionAssembler<T> getPageExtensionAssembler() { return pageExtensionAssembler; } public void setPageExtensionAssembler(PageExtensionAssembler<T> pageExtensionAssembler) { this.pageExtensionAssembler = pageExtensionAssembler; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * Returns pagination mode that will be used. */ public PaginationMode getPaginationMode() { return paginationMode; } public void setPaginationMode(PaginationMode paginationMode) { this.paginationMode = paginationMode; } } public enum PaginationMode {BASE_RECORD_LIST, FULL_LIST, PAGE_BY_IDS, PAGE_BY_INDEX_RANGE} private ListFinder<T> baseRecordListFinder; // Strategy to load base record list in base record list pagination mode private IdListFinder<K> idListFinder; // Strategy to load ids of list for page by ids pagination mode private KeyExtractor<K,T> keyExtractor; private List<ItemData<K,T>> list; // List of records or placeholders private ListFinder<T> listFinder; // Strategy to load entire list in full list pagination mode private ListSizeFinder listSizeFinder; // Strategy to get list size for page by index pagination mode private String name; // Name of list used in logging or errors private PageByIdsFinder<K,T> pageByIdsFinder; // Strategy to retrieve page in page by ids pagination mode private PageByIndexRangeFinder<T> pageByIndexRangeFinder; // Strategy to retrieve page by indices private PageExtensionAssembler<T> pageExtensionAssembler; // Adds additional data for current page for base record list pagination mode private int pageSize; private PaginationMode paginationMode; private boolean reload; // Whether list must be reloaded private int selectedIndex; // Index of currently selected record, 0 based, or -1 for no selected record /** * Copy constructor. */ public ListCache(ListCache<K,T> listCache) throws Exception { baseRecordListFinder = listCache.getBaseRecordListFinder(); idListFinder = listCache.getIdListFinder(); keyExtractor = listCache.getKeyExtractor(); list = new ArrayList<ItemData<K,T>>(listCache.list); listFinder = listCache.getListFinder(); listSizeFinder = listCache.getListSizeFinder(); name = listCache.getName(); pageByIdsFinder = listCache.getPageByIdsFinder(); pageByIndexRangeFinder = listCache.getPageByIndexRangeFinder(); pageExtensionAssembler = listCache.getPageExtensionAssembler();; pageSize = listCache.getPageSize(); paginationMode = listCache.getPaginationMode(); reload = listCache.getReload(); selectedIndex = listCache.getSelectedIndex(); } /** * @param listCacheConfig Configuration supplied by template functions. */ public ListCache(ListCacheConfig<K,T> listCacheConfig) { idListFinder = listCacheConfig.getIdListFinder(); keyExtractor = listCacheConfig.getKeyExtractor(); list = new ArrayList<ItemData<K,T>>(); listFinder = listCacheConfig.getListFinder(); listSizeFinder = listCacheConfig.getListSizeFinder(); name = listCacheConfig.getName(); pageByIdsFinder = listCacheConfig.getPageByIdsFinder(); pageByIndexRangeFinder = listCacheConfig.getPageByIndexRangeFinder(); pageExtensionAssembler = listCacheConfig.getPageExtensionAssembler();; pageSize = listCacheConfig.getPageSize(); paginationMode = listCacheConfig.getPaginationMode(); reload = true; selectedIndex = -1; } /** * Checks all list data has been loaded, not just placeholders such as base record or id. Records can be * deleted if the page-by-id finder can't find it. */ private void checkEntireListData() throws Exception { switch (paginationMode) { case BASE_RECORD_LIST: checkEntireListDataByBaseRecords(); break; case FULL_LIST: // Entire list already loaded break; case PAGE_BY_IDS: checkEntireListDataByIds(); break; case PAGE_BY_INDEX_RANGE: checkEntireListDataByIndexRange(); break; } } /** * Assembles any missing data for all items not loaded by the base record list finder. */ private void checkEntireListDataByBaseRecords() throws Exception { ItemData<K,T> itemData; T item; int size, i; Collection<T> loadingItems; size = list.size(); loadingItems = new ArrayList<>(); for (i = 0; i < size; i++) { itemData = list.get(i); item = itemData.getItem(); loadingItems.add(item); } pageExtensionAssembler.assemblePageExtensions(loadingItems); for (i = 0; i < size; i++) { itemData = list.get(i); itemData.setPageExtensionReload(false); } } /** * Loads any missing records from their ids. */ private void checkEntireListDataByIds() throws Exception { ItemData<K,T> itemData; K id; T item; Collection<K> ids; Collection<T> loadingItems; List<Integer> removalIndices; Map<K,Integer> loadingIds; int i, index, size; size = list.size(); // Find items that need to be loaded ids = new ArrayList<K>(); loadingIds = new HashMap<K,Integer>(); for (i = 0; i < size; i++) { itemData = list.get(i); if (itemData.getReload()) { ids.add(itemData.getId()); loadingIds.put(itemData.getId(), i); } } // Load items from page-by-id finder loadingItems = pageByIdsFinder.getItems(ids); for (T loadingItem: loadingItems) { id = keyExtractor.getKey(loadingItem); index = loadingIds.get(id); itemData = list.get(index); itemData.setItem(loadingItem); } // Delete items that page-by-id finder did not load as they no longer exist removalIndices = new ArrayList<Integer>(); for (K loadingId: loadingIds.keySet()) { index = loadingIds.get(loadingId); item = list.get(index).getItem(); if (item == null) { removalIndices.add(index); } } Collections.sort(removalIndices, Collections.reverseOrder()); for (int removeIndex: removalIndices) { list.remove(removeIndex); } } /** * Loads any missing records from their indices. */ private void checkEntireListDataByIndexRange() throws Exception { ItemData<K,T> itemData; T item; List<T> loadingItemList; int i, size, lowestNotLoaded, highestNotLoaded; size = list.size(); // Find what needs to be loaded. If that's multiple ranges, one big load is done anyway as it's probably faster // than multiple loads lowestNotLoaded = -1; highestNotLoaded = -1; for (i = 0; i < size; i++) { if (list.get(i).getReload()) { if (lowestNotLoaded < 0) { lowestNotLoaded = i; } highestNotLoaded = i; } } if (lowestNotLoaded > -1) { loadingItemList = pageByIndexRangeFinder.getItems(lowestNotLoaded, highestNotLoaded); // If result size differs from reported size, shrink or grow list to fit if (size > loadingItemList.size()) { list.subList(loadingItemList.size(), size).clear(); } if (size < loadingItemList.size()) { for (i = size; i < loadingItemList.size(); i++) { itemData = new ItemData<K,T>(); itemData.setId(null); itemData.setItem(null); itemData.setReload(true); list.add(itemData); } } // Load items from page-by-index-range finder for (i = 0; i < loadingItemList.size(); i++) { item = loadingItemList.get(i); itemData = list.get(i); itemData.setId(keyExtractor.getKey(item)); itemData.setItem(item); } } } /** * Loads any missing data for items in a range of indices, usually the current page. Records can be deleted if in * page-by-id mode and the page-by-id page finder can't find the required records. */ private void checkPageListData(int startIndex, int endIndex) throws Exception { switch (paginationMode) { case BASE_RECORD_LIST: checkPageListDataByBaseRecords(startIndex, endIndex); break; case FULL_LIST: // Entire list already loaded break; case PAGE_BY_IDS: checkPageListDataByIds(startIndex, endIndex); break; case PAGE_BY_INDEX_RANGE: checkPageListDataByIndexRange(startIndex, endIndex); break; } } /** * Assembles any missing data for items in a range of indices, usually the current page, not loaded by the base * record list finder. */ private void checkPageListDataByBaseRecords(int startIndex, int endIndex) throws Exception { ItemData<K,T> itemData; T item; int size, i, workingStartIndex, workingEndIndex; Collection<T> loadingItems; size = list.size(); workingStartIndex = (startIndex >= 0 && startIndex < size)?startIndex:0; workingEndIndex = (endIndex >= 0 && endIndex < size)?endIndex:(size-1); loadingItems = new ArrayList<>(); for (i = workingStartIndex; i <= workingEndIndex; i++) { itemData = list.get(i); item = itemData.getItem(); if (itemData.getPageExtensionReload()) { loadingItems.add(item); } } if (loadingItems.size() > 0) { pageExtensionAssembler.assemblePageExtensions(loadingItems); for (i = workingStartIndex; i <= workingEndIndex; i++) { itemData = list.get(i); itemData.setPageExtensionReload(false); } } } /** * Loads any missing records for items in a range of indices, usually the current page, from their ids. */ private void checkPageListDataByIds(int startIndex, int endIndex) throws Exception { ItemData<K,T> itemData; Collection<K> ids; Collection<T> loadingItems; List<Integer> removalIndices; Map<K,Integer> loadingIds; K id; T item; int i, index, listSize; boolean loadFailed; do { loadFailed = false; listSize = list.size(); // Find items that need to be loaded ids = new ArrayList<K>(); loadingIds = new HashMap<K,Integer>(); for (i = startIndex; i <= endIndex; i++) { if (i >= 0 && i <= listSize - 1) { itemData = list.get(i); if (itemData.getReload()) { ids.add(itemData.getId()); loadingIds.put(itemData.getId(), i); } } } if (ids.size() > 0) { // Load items from multi item finder loadingItems = pageByIdsFinder.getItems(ids); for (T loadingItem: loadingItems) { id = keyExtractor.getKey(loadingItem); index = loadingIds.get(id); list.get(index).setItem(loadingItem); } // Delete items that multi item finder did not load as they no longer exist removalIndices = new ArrayList<Integer>(); for (K loadingId: loadingIds.keySet()) { index = loadingIds.get(loadingId); item = list.get(index).getItem(); if (item == null) { removalIndices.add(index); loadFailed = true; } } Collections.sort(removalIndices, Collections.reverseOrder()); for (int removalIndex: removalIndices) { list.remove(removalIndex); } } } // If there any failed items, which are removed, higher entries may // shift to take their place in the requested page and some may be unloaded. while (loadFailed); } /** * Loads any missing records for items in a range of indices, usually the current page, from the indices. */ private void checkPageListDataByIndexRange(int startIndex, int endIndex) throws Exception { ItemData<K,T> itemData; T item; List<T> loadingItemList; int i, listSize, rangeSize, lowestNotLoaded, highestNotLoaded; listSize = list.size(); // Find what needs to be loaded. If that's multiple ranges, one big load is done anyway as it's probably faster // multiple loads lowestNotLoaded = -1; highestNotLoaded = -1; for (i = startIndex; i <= endIndex; i++) { if (list.get(i).getReload()) { if (lowestNotLoaded < 0) { lowestNotLoaded = i; } highestNotLoaded = i; } } if (lowestNotLoaded > -1) { rangeSize = highestNotLoaded - lowestNotLoaded + 1; loadingItemList = pageByIndexRangeFinder.getItems(lowestNotLoaded, highestNotLoaded); // If result size less than requested range, end of source list unexpectedly reached, so shrink our list if (rangeSize > loadingItemList.size()) { list.subList(startIndex + loadingItemList.size(), listSize).clear(); } // If result size greater than requested range, WTF?, ignore the excess // Load items from page-by-index-range finder for (i = 0; i < loadingItemList.size() && i < rangeSize; i++) { item = loadingItemList.get(i); itemData = list.get(i + lowestNotLoaded); itemData.setId(keyExtractor.getKey(item)); itemData.setItem(item); } } } /** * Checks list has been loaded and accessed. This is used by functions that change records as changing a record * without checking it even exists is an error. */ private void checkReloadAccess() throws IllegalStateException { if (reload) { throw new IllegalStateException("Attempt to blindly set selected record for " + name + " list without accessing list first."); } } /** * Loads list if not yet loaded or reload requested. */ private void checkReloadState() throws Exception { if (reload) { loadList(); } } /** * Ensures selected item index is within list boundary or -1 for empty list. */ private void checkSelectedIndex() { int size; size = list.size(); if (selectedIndex >= size) { selectedIndex = size - 1; } else if (selectedIndex < 0 && size > 0) { selectedIndex = 0; } } /** * Returns cached, record list from session, if it is present. */ private List<T> getListUnchecked() { List<T> result; result = new ArrayList<T>(list.size()); for (ItemData<K,T> itemData: list) { result.add(itemData.getItem()); } return result; } /** * Loads records or placeholders, according to pagination mode. */ private void loadList() throws Exception { List<K> idList; List<T> itemList; int listSize; switch (paginationMode) { case BASE_RECORD_LIST: itemList = baseRecordListFinder.getList(); if (itemList == null) { itemList = new ArrayList<T>(); } setList(itemList, false); break; case FULL_LIST: itemList = listFinder.getList(); if (itemList == null) { itemList = new ArrayList<T>(); } setList(itemList, true); break; case PAGE_BY_IDS: idList = idListFinder.getIds(); if (idList == null) { idList = new ArrayList<K>(); } setIdList(idList); break; case PAGE_BY_INDEX_RANGE: listSize = this.listSizeFinder.getSize(); setListSize(listSize); break; } } /** * Caches page-by-id record markers, which are usually the record primary keys. */ private synchronized void setIdList(List<K> ids) { ItemData<K,T> itemData; // Save list data and set control flags reload = false; // Set and save item control flags list = new ArrayList<ItemData<K,T>>(ids.size()); for (K id: ids) { itemData = new ItemData<K,T>(); itemData.setId(id); itemData.setItem(null); itemData.setPageExtensionReload(false); itemData.setReload(true); list.add(itemData); } checkSelectedIndex(); } /** * Caches new, record list. */ private synchronized void setList(List<T> value, boolean pageExtensionsLoaded) { ItemData<K,T> itemData; // Save list data and set control flags reload = false; // Set and save item control flags list = new ArrayList<ItemData<K,T>>(value.size()); for (T item: value) { itemData = new ItemData<K,T>(); itemData.setId(keyExtractor.getKey(item)); itemData.setItem(item); itemData.setPageExtensionReload(!pageExtensionsLoaded); itemData.setReload(false); list.add(itemData); } checkSelectedIndex(); } /** * Caches page-by-index-range record markers. */ private synchronized void setListSize(int listSize) { ItemData<K,T> itemData; int i; // Save list data and set control flags reload = false; // Set and save item control flags list = new ArrayList<ItemData<K,T>>(listSize); for (i = 0; i < listSize; i++) { itemData = new ItemData<K,T>(); itemData.setId(null); itemData.setItem(null); itemData.setPageExtensionReload(false); itemData.setReload(true); list.add(itemData); } checkSelectedIndex(); } /** * Replaces selected record of list. Current, selected item index cannot be -1. */ private void setSelectedItemUnchecked(T item) { list.get(selectedIndex).setItem(item); } /** * Adds new item to end of list and sets it as selected item. */ public synchronized void addItem(T item) throws IllegalStateException, Exception { ItemData<K,T> itemData; checkReloadAccess(); itemData = new ItemData<K,T>(); itemData.setId(keyExtractor.getKey(item)); itemData.setItem(item); itemData.setPageExtensionReload(false); itemData.setReload(false); list.add(itemData); // Change selected index to refer to new item setSelectedIndex(list.size() - 1); } /** * Reloads list using currently set list finder, id list finder, or list size finder. */ public synchronized void forceReload() throws Exception { list.clear(); reload = true; loadList(); } /** * Returns Strategy object used to retrieve list. */ public ListFinder<T> getBaseRecordListFinder() { return baseRecordListFinder; } /** * Returns Strategy that extracts the primary key from a record. */ public KeyExtractor<K,T> getKeyExtractor() { return keyExtractor; } /** * Returns Strategy used to retrieve list. */ public IdListFinder<K> getIdListFinder() { return idListFinder; } /** * Returns entire record list, loading it if needed. */ public synchronized List<T> getList() throws Exception { checkReloadState(); checkEntireListData(); return getListUnchecked(); } /** * Returns Strategy object used to retrieve list. */ public ListFinder<T> getListFinder() { return listFinder; } /** * Returns list size. */ public synchronized int getListSize() throws Exception { checkReloadState(); return list.size(); } /** * Returns Strategy object used to retrieve list size. */ public ListSizeFinder getListSizeFinder() { return listSizeFinder; } public String getName() { return name; } /** * Returns current page containing selected record, loading records if needed. */ public synchronized List<T> getPage() throws Exception { int pageNo, startIndex, endIndex; checkReloadState(); if (list.size() > 0) { pageNo = getPageNo(); pageNo = (pageNo < 1)?1:pageNo; startIndex = (pageNo - 1) * pageSize; endIndex = startIndex + pageSize - 1; endIndex = (endIndex >= list.size())?list.size() - 1:endIndex; checkPageListData(startIndex, endIndex); // checkPageListData can shrink the list as it discovers items no longer exist startIndex = (startIndex >= list.size())?list.size() - 1:startIndex; startIndex = (startIndex < 0)?0:startIndex; endIndex = (endIndex >= list.size())?list.size() - 1:endIndex; return getListUnchecked().subList(startIndex, endIndex + 1); } else { return new ArrayList<T>(); } } /** * Returns Strategy object used to retrieve page by their record primary keys. */ public PageByIdsFinder<K,T> getPageByIdsFinder() { return pageByIdsFinder; } /** * Returns Strategy object used to retrieve page by their indices. */ public PageByIndexRangeFinder<T> getPageByIndexRangeFinder() { return pageByIndexRangeFinder; } /** * Returns Transfer Object Assembler for adding record extensions needed for requested page that is not loaded * by a base record list finder. Only applies for base record list pagination mode. */ public PageExtensionAssembler<T> getPageExtensionAssembler() { return pageExtensionAssembler; } /** * Returns number of current page, starting at 1, containing selected record. */ public synchronized int getPageNo() throws Exception { if (selectedIndex > -1) { return (selectedIndex / pageSize) + 1; } else { return 0; } } public int getPageSize() throws Exception { return pageSize; } /** * Returns how pages are retrieved. */ public PaginationMode getPaginationMode() { return paginationMode; } /** * Returns range of records from cached list, loading them if needed. If the start or end are out of range, they * are treated as the index of the nearest item. Thus, at least one item is always returned unless no items exist * at all. */ public synchronized List<T> getRange(int startIndex, int endIndex) throws Exception { int workingStart, workingEnd; checkReloadState(); if (list.size() > 0) { workingStart = startIndex; workingStart = (workingStart < 0)?0:workingStart; workingStart = (workingStart >= list.size())?list.size() - 1:workingStart; workingStart = (workingStart < 0)?0:workingStart; workingEnd = endIndex; workingEnd = (workingEnd < 0)?0:workingEnd; workingEnd = (workingEnd >= list.size())?list.size() - 1:workingEnd; workingEnd = (workingEnd < 0)?0:workingEnd; checkPageListData(workingStart, workingEnd); // checkPageListData can shrink the list as it discovers items no longer exist workingStart = (workingStart >= list.size())?list.size() - 1:workingStart; workingStart = (workingStart < 0)?0:workingStart; workingEnd = (workingEnd >= list.size())?list.size() - 1:workingEnd; return getListUnchecked().subList(workingStart, workingEnd + 1); } else { return new ArrayList<T>(); } } /** * Indicates if record list must be reloaded. */ public boolean getReload() { return reload; } /** * Returns selected item from list. * @throws IllegalStateException List finder not set and reload required. */ public synchronized T getSelected() throws Exception { checkReloadState(); if (selectedIndex >= 0) { checkPageListData(selectedIndex,selectedIndex); return list.get(selectedIndex).getItem(); } else { return null; } } /** * Returns index of currently selected item, starting at 0, in list. */ public synchronized int getSelectedIndex() throws Exception { checkReloadState(); return selectedIndex; } public synchronized int getTotalPages() { return list.size()/pageSize + (list.size()%pageSize == 0?0:1); } /** * Marks record list as requiring a reload. */ public synchronized void markReload() { list.clear(); reload = true; } /** * Removes item from list at index position and returns it. Returns null if no item exists at index. * * @throws IllegalStateException List must be reloaded but list finder is missing. * @throws Exception Failed to retrieve extensions. */ public synchronized T remove(int index) throws IllegalStateException, Exception { ItemData<K, T> itemData; T result; result = null; checkReloadAccess(); itemData = list.get(index); if (itemData != null) { list.remove(index); checkSelectedIndex(); result = itemData.getItem(); } return result; } /** * Removes currently, selected item from list and returns it. Returns null if the list is empty or there is no * selected item. */ public synchronized T removeSelected() throws IllegalStateException, Exception { T result; checkReloadAccess(); if (selectedIndex >= 0) { result = list.get(selectedIndex).getItem(); list.remove(selectedIndex); checkSelectedIndex(); } else { result = null; } return result; } /** * Caches list already loaded, sets base record list finder Strategy used to load it, and sets base record list pagination mode. */ public synchronized void setBaseRecordListAndFinder(List<T> list, ListFinder<T> baseRecordListFinder) { if (baseRecordListFinder == null) { throw new IllegalStateException("List finder for " + name + " list must be set with its list."); } setList(list, false); this.baseRecordListFinder = baseRecordListFinder; this.idListFinder = null; this.listFinder = null; this.listSizeFinder = null; this.pageByIndexRangeFinder = null; this.paginationMode = PaginationMode.BASE_RECORD_LIST; setSelectedIndex(0); } /** * Sets base record list finder, marks list for reload, and sets base record list pagination mode.. */ public void setBaseRecordListFinder(ListFinder<T> baseRecordListFinder) { if (baseRecordListFinder == null) { throw new IllegalStateException("Base record list finder for " + name + " list must be set."); } setList(new ArrayList<>(), false); this.baseRecordListFinder = baseRecordListFinder; this.idListFinder = null; this.listFinder = null; this.listSizeFinder = null; this.pageByIndexRangeFinder = null; this.paginationMode = PaginationMode.BASE_RECORD_LIST; markReload(); } /** * Caches id list already loaded, sets id list finder Strategy used to load it, and sets page by ids pagination mode. */ public synchronized void setIdListAndFinder(List<K> idList, IdListFinder<K> idListFinder) { if (idListFinder == null) { throw new IllegalStateException("Id list finder for " + name + " list must be set with its id list."); } setIdList(idList); this.baseRecordListFinder = null; this.idListFinder = idListFinder; this.listFinder = null; this.listSizeFinder = null; this.pageByIndexRangeFinder = null; this.paginationMode = PaginationMode.PAGE_BY_IDS; setSelectedIndex(0); } /** * Sets Strategy used to retrieve ids, sets page by ids pagination mode and marks list for reload. */ public void setIdListFinder(IdListFinder<K> idListFinder) { if (idListFinder == null) { throw new IllegalStateException("Id list finder for " + name + " list must be set with its id list."); } setIdList(new ArrayList<>()); this.baseRecordListFinder = null; this.idListFinder = idListFinder; this.listFinder = null; this.listSizeFinder = null; this.pageByIndexRangeFinder = null; this.paginationMode = PaginationMode.PAGE_BY_IDS; markReload(); } /** * Caches list already loaded, sets full list Strategy used to load it, and sets full list pagination mode. */ public synchronized void setListAndFinder(List<T> list, ListFinder<T> listFinder) { if (listFinder == null) { throw new IllegalStateException("List finder for " + name + " list must be set with its list."); } setList(list, true); this.baseRecordListFinder = null; this.idListFinder = null; this.listFinder = listFinder; this.listSizeFinder = null; this.pageByIndexRangeFinder = null; this.paginationMode = PaginationMode.FULL_LIST; setSelectedIndex(0); } /** * Sets full list Strategy to retrieve list when needed and sets full list pagination mode. */ public synchronized void setListFinder(ListFinder<T> listFinder) { if (listFinder == null) { throw new IllegalStateException("List finder for " + name + " list must be set with its list."); } setList(new ArrayList<>(), true); this.baseRecordListFinder = null; this.idListFinder = null; this.listFinder = listFinder; this.listSizeFinder = null; this.pageByIndexRangeFinder = null; this.paginationMode = PaginationMode.FULL_LIST; markReload(); } /** * Sets list size, the list size finder used to find that, page by index range finder that will be used to find * pages from list, and sets page by index pagination mode. * * @param listSize Size of list to cache. * @param listSizeFinder Command that found list size. * @param pageByIndexRangeFinder Command that will load pages of list.. */ public synchronized void setListSizeAndFinder(int listSize, ListSizeFinder listSizeFinder, PageByIndexRangeFinder<T> pageByIndexRangeFinder) { if (listSizeFinder == null || pageByIndexRangeFinder == null) { throw new IllegalStateException("List size finder pr page by index tange finder for " + name + " list must be set with its list size."); } setListSize(listSize); this.baseRecordListFinder = null; this.idListFinder = null; this.listFinder = null; this.listSizeFinder = listSizeFinder; this.pageByIndexRangeFinder = pageByIndexRangeFinder; this.paginationMode = PaginationMode.PAGE_BY_INDEX_RANGE; setSelectedIndex(0); } /** * Sets the list size finder, sets accompanying page by index range finder that will be used to find pages from list, * sets page by index pagination mode, and marks list for reload. * * @param listSizeFinder Command that found list size. * @param pageByIndexRangeFinder Command that will load pages of list.. */ public void setListSizeFinder(ListSizeFinder listSizeFinder, PageByIndexRangeFinder<T> pageByIndexRangeFinder) { if (listSizeFinder == null || pageByIndexRangeFinder == null) { throw new IllegalStateException("List size finder pr page by index tange finder for " + name + " list must be set with its list size."); } setListSize(0); this.baseRecordListFinder = null; this.idListFinder = null; this.listFinder = null; this.listSizeFinder = listSizeFinder; this.pageByIndexRangeFinder = pageByIndexRangeFinder; this.paginationMode = PaginationMode.PAGE_BY_INDEX_RANGE; markReload(); } /** * Sets Strategy used to retrieve page by their record ids. */ public void setPageByIdsFinder(PageByIdsFinder<K,T> value) { pageByIdsFinder = value; } /** * Sets Transfer Object Assembler for adding record extensions needed for requested page that is not loaded * by a base record list finder. Only applies for base record list pagination mode. */ public void setPageExtensionAssembler(PageExtensionAssembler<T> pageExtensionAssembler) { this.pageExtensionAssembler = pageExtensionAssembler; } /** * Sets index of currently selected item to be first item of page where pageNo starts at one. */ public void setPageNo(int pageNo) { selectedIndex = (pageNo - 1) * pageSize; checkSelectedIndex(); } public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * Replaces selected item in list. */ public void setSelected(T value) { checkReloadAccess(); if (selectedIndex != -1) { setSelectedItemUnchecked(value); } } /** * Sets currently selected item in list by index. Sets to last item if index exceeds last item. Sets no currently * selected item if index is -1. */ public synchronized void setSelectedIndex(int index) { int size; checkReloadAccess(); size = list.size(); if (index >= size) { selectedIndex = size - 1; } else if (index < 0) { selectedIndex = -1; } else { selectedIndex = index; } } }